/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CommonProfile;

import CRUD.BranchEnterpriseFacadeLocal;
import CRUD.EnterpriseFacadeLocal;
import CRUD.PersonalInfoFacadeLocal;
import Entity.BranchEnterprise;
import Entity.Enterprise;
import Entity.PersonalInfo;
import java.math.BigInteger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author WinKiQue58
 */
@Stateless
public class PersonalInformation implements PersonalInformationLocal {
   
    @PersistenceContext(unitName = "S3R4_Orcl-ejbPU")
     private EntityManager em;
    
     @EJB private PersonalInfoFacadeLocal personalInfoFacade;
     @EJB  private BranchEnterpriseFacadeLocal branchEnterpriseFacade;
     @EJB  private EnterpriseFacadeLocal enterpriseFacade;
     
    public boolean UpdatePersonalInformation (String name, String RealName, String Pass, 
            String email,String Cellphone, String Address, String Phone, String Multilevel,
            String EnterpriseSubject, String ACTUALUSER )
    {
       try{
        
        //--Name is unique!
        Query q1= em.createQuery("SELECT pi FROM PersonalInfo pi WHERE pi.user = :NAME");
        q1.setParameter("NAME", ACTUALUSER);
        PersonalInfo pi= (PersonalInfo) q1.getSingleResult();
  
        if (name.length()>0) pi.setS3r4User(name);
        if (RealName.length()>0) pi.setName(RealName);
        if (Pass.length()>0) pi.setPass(Pass);
        if (email.length()>0) pi.setEmail(email);
        if (Cellphone.length()>0) pi.setCellphone(new BigInteger (Cellphone) );
        if (Address.length()>0) pi.setAddress(Address);
        if (Phone.length()>0) pi.setPhone(new BigInteger(Phone));
        
        personalInfoFacade.edit(pi);
       
        //-If is is an Administrator
        if (pi.getUsertype()==2){
            if (EnterpriseSubject.length()>0){
            Query q2= em.createQuery("SELECT e FROM Enterprise e WHERE e.idEnterprise = :ID");
            q2.setParameter("ID", pi.getPersonalInfoPK().getIdEnterprise() );
            Enterprise e =(Enterprise) q2.getSingleResult();
            e.setName(EnterpriseSubject);

            enterpriseFacade.edit(e);
            }

            if (Multilevel.length()>0){ 
            Query q3= em.createQuery("SELECT be FROM BranchEnterprise be WHERE "
                    + "be.branchEnterprisePK.idBranchenterprise = :A AND "
                    + "be.branchEnterprisePK.idEnterprise = :B");    
            q3.setParameter("A", pi.getPersonalInfoPK().getIdEnterprise() );
            q3.setParameter("B", pi.getPersonalInfoPK().getIdBranchenterprise());
            BranchEnterprise be=(BranchEnterprise)q3.getSingleResult();
            be.setMultilevel(Integer.parseInt(Multilevel));

            branchEnterpriseFacade.edit(be);
            }
        }
              
        
        }catch (Exception e)
        {
           return false;
        }
        return true;
    }
    
    
    
    
}
